﻿Imports Cdm.DataCore.Entities
Partial Class ParticipantForm_Upload
    Inherits System.Web.UI.Page

    Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UploadButton.Click
        If Me.FormUpload.HasFile Then
            Try
                Dim uploader As New Spreadsheet.ParticipantFormUploader()
                Dim uploadParticipant As Cdm.DataCore.Entities.Participant = uploader.UploadPopulatedForm(FormUpload.FileContent)
                If uploadParticipant Is Nothing Then
                    PopupMessage1.ShowErrorMessage("Can not submit file", "An Error occurred submitting your file")
                Else
                    EntityController.SetParticipant(uploadParticipant)
                    DisplayConfirmation(uploadParticipant)

                End If
            Catch ex As Exception
                PopupMessage1.ShowErrorMessage("Error Submitting File", ex.Message)
            End Try
        Else
            PopupMessage1.ShowErrorMessage("No file attached", "Please Browse to a file to submit.")
        End If
    End Sub

    Private Sub DisplayConfirmation(ByVal pt As Participant)
        Dim newText = "Upload Successful.  This document appears to be for a new participant: {0}.  Do you wish to proceed?"
        Dim existText = "Upload Successful.  This document appears to be for an existing participant: {0}.  Do you wish to proceed"""
        If pt.ParticipantId = 0 Then
            ConfirmLabel.Text = String.Format(newText, pt.FullName)
        Else
            Dim currParticipant As Participant = Participant.Retrieve(pt.ParticipantId)
            ConfirmLabel.Text = String.Format(existText, currParticipant.FullName)
        End If
        Me.ModalPopupConfirm.Show()
    End Sub

    Private Function GetSearchParticipantId() As Integer
        Try
            Dim parenStart As Integer = ParticipantSearchTextBox.Text.LastIndexOf("(")
            Dim parenEnd As Integer = ParticipantSearchTextBox.Text.LastIndexOf(")")
            If parenEnd = -1 Or parenStart = -1 Then
                Return 0
            End If

            If parenEnd < parenStart Then Return 0

            Return CInt(ParticipantSearchTextBox.Text.Substring(parenStart + 1, parenEnd - parenStart - 1))
        Catch
            Return 0
        End Try
    End Function

    Protected Sub DownloadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DownloadButton.Click
        Dim participantId As Integer = GetSearchParticipantId()
        Dim blankForm As String
        Dim completedForm As String = ""

        blankForm = Server.MapPath(System.Web.Configuration.WebConfigurationManager.AppSettings("ParticipantDocumentFileName"))

        If participantId = 0 Then
            PopupMessage1.ShowErrorMessage("Can not Create Form", "Participant not identified")
        Else
            Dim downloader As New Spreadsheet.ParticipantFormDownloader()
            Dim imageName As String = ""
            Dim applicationName As String = Application("AppName")
            Dim deptName As String = Application("StateDeptName")
            If Not Application("CurrentLogo") Is Nothing Then
                imageName = Server.MapPath(Application("CurrentLogo"))
            End If
            completedForm = downloader.DownloadPopulatedForm(participantId, blankForm, _
                                                applicationName, imageName, deptName)
            Dim currParticipant As Participant = Participant.Retrieve(participantId)
            Response.ContentType = "application/vnd.ms-excel"
            'Response.AppendHeader("Content-Disposition", "filename=Data.xlsx;");
            Dim fileHeader As String = String.Format("attachment;filename=ParticipantForm_{0}.xls", currParticipant.FullName.Replace(" ", ""))
            Response.AddHeader("Content-Disposition", fileHeader)
            Response.AddHeader("Target", "_blank")
            Response.WriteFile(completedForm)
            Response.Flush()
            System.IO.File.Delete(completedForm)
            Response.End()


        End If
    End Sub

    Protected Sub ConfirmUploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ConfirmUploadButton.Click
        Response.Redirect("~/Case/CasePersonal.aspx")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim fileName As String = System.Web.Configuration.WebConfigurationManager.AppSettings("ParticipantDocumentFileName")
            Me.BlankFormHyperLink.NavigateUrl = fileName
        End If
    End Sub
End Class
